package com.lx.spring.jdbc.servlet;


import com.google.gson.Gson;
import com.lx.spring.biz.BizException;
import com.lx.spring.jdbc.biz.AccountBiz;
import com.lx.spring.jdbc.entity.Account;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

//@WebServlet("/login")
public class LoginAction extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Long id = Long.valueOf(req.getParameter("name"));
        String pwd = req.getParameter("pwd");
        AccountBiz biz = new AccountBiz();
        Account user;
        try {
            user = biz.login(id, pwd);
        } catch (BizException e) {
            throw new RuntimeException(e);
        }
        if (user == null){
            return;
        }
        req.getSession().setAttribute("userInfo", user);
        String json = new Gson().toJson(user);
        resp.getWriter().append(json);


    }
}
